cmake_minimum_required (VERSION 2.8)

# BUILD_UT: build unit-test
# BUILD_EG: build examples
option (BUILD_UT "Build unit test" OFF)
option (BUILD_EG "Build project example" OFF)

set (OS "Linux" CACHE STRING "Operating system select")

if (${CMAKE_CURRENT_SOURCE_DIR} STREQUAL ${CMAKE_SOURCE_DIR})
    # prepare environment
    message(STATUS "Start download vendor sdk")
    execute_process (COMMAND chmod +x ${PROJECT_SOURCE_DIR}/vendor/vendor.sh
                     COMMAND ${PROJECT_SOURCE_DIR}/vendor/vendor.sh ${CMAKE_SOURCE_DIR}/vendor
                     RESULT_VARIABLE vendor_ret)
    if(NOT (${vendor_ret} STREQUAL "0") )
        message(FATAL_ERROR "Prepare environment failed!")
    endif()
    message(STATUS "Finish download vendor sdk")

    # download third-party repo
    message(STATUS "Start download third-party repos")
    execute_process (COMMAND chmod +x ${PROJECT_SOURCE_DIR}/3rd/3rd.sh
                     COMMAND ${PROJECT_SOURCE_DIR}/3rd/3rd.sh ${CMAKE_SOURCE_DIR}/3rd
                     RESULT_VARIABLE 3rd_ret)
    if(NOT (${3rd_ret} STREQUAL "0") )
        message(FATAL_ERROR "Download third-party failed!")
    endif()
    message(STATUS "Finish download third-party repos")

    # dependence
    message (STATUS "import modules dependence.cmake")
    include (${PROJECT_SOURCE_DIR}/cmake/dependence.cmake)
    message (STATUS "LINK_LIBS: ${LINK_LIBS}")
    list (REMOVE_DUPLICATES LINK_LIBS)
    list (REVERSE LINK_LIBS)
    message (STATUS "LINK_LIBS: ${LINK_LIBS}")

endif()

# build 3rd
add_subdirectory(3rd)
# build vendor
add_subdirectory(vendor)
# build current module
add_subdirectory(source)

if (${BUILD_EG} STREQUAL "ON")
    add_subdirectory (examples)
endif()
if (${BUILD_UT} STREQUAL "ON")
    add_subdirectory (unittest)
endif()

# uninstall target
if (NOT TARGET uninstall)
    configure_file (
        "${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in"
        "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
        IMMEDIATE @ONLY)

    add_custom_target (uninstall
        COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
endif()

